/*************************************************************************
	> File Name: 012.实现没有Bug的MAX宏.c
	> Author: Maureen 
	> Mail: Maureen@qq.com 
	> Created Time: 三  8/25 19:25:58 2021
 ************************************************************************/

#include <stdio.h>

//保证表达式完全被展开了
#define MAX(a, b) ( { \
    __typeof(a) _a = a; \
    __typeof(b) _b = b; \
    _a > _b ? _a : _b; \
})

//#a表示将表达式字符串化
#define P(a) { \
    printf("%s = %d\n", #a, a); \
}

int main() {
    int a = 7;
    P(MAX(2, 3));
    P(5 + MAX(2, 3));
    P(MAX(2, MAX(3, 4)));
    P(MAX(2, 3 > 4 ? 3 : 4));
    P(MAX(a++, 6));
    P(a);
    return 0;
}

//运行结果：
//MAX(2, 3) = 3
//5 + MAX(2, 3) = 8
//MAX(2, MAX(3, 4)) = 4
//MAX(2, 3 > 4 ? 3 : 4) = 4
//MAX(a++, 6) = 7
//a = 8
